//
//  GuestCouponSearchViewController.m
//  penzer
//
//  Created by 还带大道 on 2018/11/6.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "GuestCouponSearchViewController.h"
#import "GuestCouponHomeViewController.h"
#import "GuestSearchRecordView.h"

@interface GuestCouponSearchViewController ()<UITextFieldDelegate,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *searchV;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topV_topMargin;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextField *textFiled;
@property (nonatomic,strong) GuestSearchRecordView *footerV;

@end

@implementation GuestCouponSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigation:YES andTitle:@"搜索" andNaviColor:[UIColor whiteColor] andTitleColor:nil];
    self.topV_topMargin.constant = kTopHeight;
    self.searchV.backgroundColor = RZ_COLOR(245, 245, 245);
    self.searchV.layer.cornerRadius = 2;
    [self.textFiled becomeFirstResponder];
    [self setupTableView];
}


- (void)setupTableView
{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIView *headerV = [UIView new];
    headerV.backgroundColor = [UIColor whiteColor];
    headerV.height = 40;
    UILabel *label = [UILabel new];
    label.text = @"搜索历史";
    label.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:18];
    label.textColor = RZ_COLOR(51, 51, 51);
    [headerV addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(headerV);
        make.left.mas_equalTo(15);
    }];
    self.tableView.tableHeaderView = headerV;
    
    GuestSearchRecordView *footerV = [GuestSearchRecordView new];
    _footerV = footerV;
    footerV.itemOnClickBlock = ^(NSString *str) {
        [self gotoSearchResultWithKeywords:str];
    };
    self.tableView.tableFooterView = footerV;
}




#pragma mark--UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
    [textField resignFirstResponder];
    [self.footerV updateWithKeywords:textField.text];
    [self gotoSearchResultWithKeywords:textField.text];
    return YES;
}



- (void)gotoSearchResultWithKeywords:(NSString *)keywords
{
    GuestCouponHomeViewController *coupon = [[GuestCouponHomeViewController alloc] initWithViewType:CouponViewTypeSearchResult];
    coupon.keywords = keywords;
    [self pushSecondVC:coupon];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}


- (IBAction)cancel {
    [self.navigationController popViewControllerAnimated:YES];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 0;
}

@end
